package com.example.demo.jdbc.custom;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class ExistsById extends AbstractMethod {
    private static final String SQL_SCRIPT = "select exists(select 1 from %s where %s = #{%s});";
    
    private static final String SQL_METHOD = "existsById";
    
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        final String sql = String.format(SQL_SCRIPT, tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
        final SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return addSelectMappedStatementForOther(mapperClass, SQL_METHOD, sqlSource, Boolean.class);
    }
    
}
